<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/dataCharge.css" rel="stylesheet" type="text/css"/>
<title>Home</title>
</head>
<body>
<?php
	$user = $_POST['user'];
	$date = date("Y-m-d", strtotime($_POST['date']));
	
	include_once('frontend/header.php');
?>
	
    
<div class="container">
     <br /><br /> 

<?php
	include_once('functions/dataBaseConnection.php');
	$succesfull = true;
	//carga de horarios de proyectos
	$projects = array_keys($_POST['projects']);
	foreach ($projects as $eachProject) {
		$hours = $_POST['projects'][$eachProject];
		$result = mysqli_query($connection, "SELECT code FROM projects WHERE name = '$eachProject'");
		$project = mysqli_fetch_array($result);
		$query = "INSERT INTO  usersworkedtime (user, date, project, hours) VALUES ('$user', '$date' , '$project[0]' , '$hours')";
		$result = mysqli_query($connection, $query);
		if (!$result) {
			$succesfull = false;
		}
	}
	
	//carga de horarios de consultorias
	$consultancies = array_keys($_POST['consultancies']);
	foreach ($consultancies as $eachConsultancy) {
		$hours = $_POST['consultancies'][$eachConsultancy];
		$result = mysqli_query($connection, "SELECT code FROM consultancies WHERE name = '$eachConsultancy'");
		$consultancy = mysqli_fetch_array($result);
		$query = "INSERT INTO  usersconsultanciestime (user, date, consultancy, hours) VALUES ('$user', '$date' , '$consultancy[0]' , '$hours')";
		$result = mysqli_query($connection, $query);
		if (!$result) {
			$succesfull = false;
		}
	}
	
	//confirmacion de carga exitosa
	if ($succesfull) {
		include('functions/setLateUsers.php');
		echo "<a class='greenText'>Carga de horas exitosa</a>";
	}else{
		echo "<a class='redText'>Error en la carga. Reintente</a>";
	}		
?>
	<input value="volver" name="back" type="button" onclick="document.location.href = 'home.php'" />
</div>